package com.hongdee.insurance.quotation.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.quotation.entity.TInsuranceQuotation;
import com.hongdee.insurance.quotation.service.ITInsuranceQuotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/4/26.
 */
@RestController
@RequestMapping("/insurance/Quotation")
public class TInsuranceQuotationController {
    @Autowired
    private ITInsuranceQuotationService itInsuranceQuotationService;
    /**
     * 新增或修改报价单
     * @param uid
     * @param token
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity Save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map map) throws Exception {

        TInsuranceQuotation insuranceQuotation= null;
        if(map.get("paramQuotation")!=null){
            insuranceQuotation=JSON.toJavaObject((JSON) JSONArray.toJSON(map.get("paramQuotation")),TInsuranceQuotation.class);
        }else{
            throw new Exception("信息错误，请检查录入的信息！！");
        }
        String insurance= StringUtil.formatEmpty(map.get("insurance"));
        EntityUtils.valid(insuranceQuotation);
        TInsuranceQuotation tbxQuotation = this.itInsuranceQuotationService.saveOrUpdate(insuranceQuotation,uid,insurance);
        Map infoMap=this.itInsuranceQuotationService.getQuotationInfo(tbxQuotation.getId(),uid);
        ResultEntity res = new ResultEntity();
        res.setData(infoMap);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * 新增或修改报价单
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id", defaultValue = "") String id,
                             @RequestBody Map data) throws Exception {
        EntityUtils.valid(data);
        this.itInsuranceQuotationService.update(data,uid,id);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * 报价单分页查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itInsuranceQuotationService.page(uid,start, limit,data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * 报价单明细
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择报价单！");
        }
        Map map = itInsuranceQuotationService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 删除报价单信息
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResultEntity del(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itInsuranceQuotationService.delInsuranceById(id,false);
        res.setSuccess(true);
        res.setMsg("报价单删除成功！！");
        return res;
    }

    @RequestMapping("/checkWarning")
    @ResponseBody
    public Map checkWarning(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "carId", defaultValue = "") String carId,
                                     @RequestParam(name = "syPrice", defaultValue = "") String syPrice) throws Exception {
        Map resultMap=null;
        if(!"".equals(syPrice)) {
            Float syPriceF=Float.valueOf(syPrice);
            resultMap = itInsuranceQuotationService.checkWarningByCar(carId, syPriceF, uid);
        }else{
            resultMap=new HashMap();
            resultMap.put("msg","");
            resultMap.put("success",true);
        }

        return resultMap;
    }

    @RequestMapping("/bihu")
    @ResponseBody
    public Map bihu(@RequestParam(name = "uid",defaultValue = "")String uid,
                    @RequestParam(name = "token",defaultValue = "")String token,
                    @RequestParam(name = "plateNum",defaultValue = "")String plateNum,
                    @RequestBody Map data) throws Exception {

        if(data.get("code") != null){ // 车辆档案更新
            if(data.get("code").equals(1)){
                itInsuranceQuotationService.updateCarInfoByBIHU(uid,plateNum,data);
            }
        }
        if(data.get("Item") != null){ // 报价
            itInsuranceQuotationService.analyzeBiHuMsg(uid,plateNum,data);
        }

        Map<String,Object> map=new HashMap<>();
        map.put("success","true");
        map.put("msg","录入成功!!");
        return map;
    }

}
